//
//  WhzPDDTabBarController.m
//  WhzPinDuoDuo
//
//  Created by whz on 16/11/13.
//  Copyright © 2016年 whz. All rights reserved.
//

#import "WhzPDDTabBarController.h"
#import "WhzPDDNavigationController.h"
#import "WhzPDDHomeViewController.h"
#import "WhzPDDNewGoodsViewController.h"
#import "WhzPDDLookMoreViewController.h"
#import "WhzPDDSearchViewController.h"
#import "WhzPDDMineViewController.h"

@interface WhzPDDTabBarController ()

@end

@implementation WhzPDDTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
}

/**
 初始化UI
 */
- (void)setupUI {
    
    UIViewController *vc1 = [self createChildControllerWithClassName:@"WhzPDDHomeViewController" andTitle:@"首页" andImageName:nil];
    UIViewController *vc2 = [self createChildControllerWithClassName:@"WhzPDDNewGoodsViewController" andTitle:@"上新" andImageName:nil];
    UIViewController *vc3 = [self createChildControllerWithClassName:@"WhzPDDLookMoreViewController" andTitle:@"海淘" andImageName:nil];
    UIViewController *vc4 = [self createChildControllerWithClassName:@"WhzPDDSearchViewController" andTitle:@"搜索" andImageName:nil];
    UIViewController *vc5 = [self createChildControllerWithClassName:@"WhzPDDMineViewController" andTitle:@"个人中心" andImageName:nil];
    
    self.viewControllers = @[vc1, vc2, vc3, vc4, vc5];
    
    self.tabBar.translucent = NO;
}

#pragma mark - 添加子控制器方法(抽取)
/**
 创建子控制器

 @param className 控制器名称
 @param title title
 @param imageName imageName
 @return 控制器
 */
- (UIViewController *)createChildControllerWithClassName:(NSString *)className andTitle:(NSString *)title andImageName:(NSString *)imageName {

    Class class = NSClassFromString(className);
    UIViewController *vc = [[class alloc] init];
    
    return [self createChildNavcWithController:vc andTitle:title andImageNamed:imageName];
}

#pragma mark - 给子控制器返回一个导航控制器方法(抽取)
/**
 给子控制器返回一个导航控制器

 @param vc 子控制器
 @param title title
 @param imageName imageName
 @return 导航控制器
 */
- (UIViewController *)createChildNavcWithController:(UIViewController *)vc andTitle:(NSString *)title andImageNamed:(NSString *)imageName {

    vc.title = title;
    vc.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:[imageName stringByAppendingString:@"selected"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    return [[WhzPDDNavigationController alloc] initWithRootViewController:vc];
}

@end
